package com.xxq.aidlclient.Activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import com.xxq.aidlclient.R;

import java.util.ArrayList;
import java.util.List;

import com.xxq.aidlserver.NewsInfo;
import com.xxq.aidlserver.IAidlInterface;

public class MainActivity extends Activity{
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Context mContext;
    private ListView mListView;
    private NewsAdapter mNewsAdapter;

    private IAidlInterface iAidlService;
    private List<NewsInfo> mNewsList = new ArrayList<NewsInfo>();

    BootReceiver mBootReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_mainpage);
        mContext = this;
        initView();
        onBtnListener();

        //动态注册广播
        mBootReceiver = new BootReceiver();
        IntentFilter itFilter = new IntentFilter();
        itFilter.addAction("android.intent.action.BOOT_COMPLETED");
        registerReceiver(mBootReceiver, itFilter);
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(android.os.Message msg){
            if(msg.what == 0x123){
                mNewsAdapter = new NewsAdapter(mContext, R.layout.item_news_list, mNewsList);
                mListView.setAdapter(mNewsAdapter);
            }
        }
    };

    private void initView(){
        btn1 = (Button)findViewById(R.id.button1);
        btn2 = (Button)findViewById(R.id.button2);
        btn3 = (Button)findViewById(R.id.button3);
        btn4 = (Button)findViewById(R.id.button4);
        mListView = (ListView)findViewById(R.id.news_listView);
    }

    private void onBtnListener(){
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindService();
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unBindService();
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        getNews();
                        mHandler.sendEmptyMessage(0x123);
                    }
                }).start();
            }
        });
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearNews();
            }
        });
    }

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder iBinder) {
            Log.d("AidlClient","onServiceConnected()");
            iAidlService = IAidlInterface.Stub.asInterface(iBinder);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            iAidlService = null;
        }
    };

    //绑定远程服务
    private void bindService(){
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.xxq.aidlserver",
                "com.xxq.aidlserver.AidlService"));
        bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
    }

    private void unBindService(){
        if(mServiceConnection!=null){
            unbindService(mServiceConnection);
            mServiceConnection = null;
        }
    }

    private void getNews() {
        try{
            if (iAidlService != null) {
                mNewsList = iAidlService.getNewsList();
                System.out.println("mNewsList.size= "+mNewsList.size());
            } else {
                System.out.println("iAidlService is null");
            }
        } catch(RemoteException e){
            e.printStackTrace();
        }
    }

    private void clearNews(){
        if(!mNewsList.isEmpty()){
            mNewsList.clear();
            mNewsAdapter = new NewsAdapter(mContext, R.layout.item_news_list, mNewsList);
            mListView.setAdapter(mNewsAdapter);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBootReceiver);
    }
}
